package cn.com.egova.erp.action;

import cn.com.egova.erp.biz.IOrderdetailBiz;
import cn.com.egova.erp.entity.Emp;
import cn.com.egova.erp.entity.Orderdetail;
import cn.com.egova.erp.entity.Store;
import cn.com.egova.erp.exception.ErpException;
import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * 订单明细Action
 *
 * @author Administrator
 */
public class OrderdetailAction extends BaseAction<Orderdetail> {

    private IOrderdetailBiz orderdetailBiz;

    private Long storeuuid;

    public void setOrderdetailBiz(IOrderdetailBiz orderdetailBiz) {
        this.orderdetailBiz = orderdetailBiz;
        super.setBaseBiz(this.orderdetailBiz);
    }

    public Long getStoreuuid() {
        return storeuuid;
    }

    public void setStoreuuid(Long storeuuid) {
        this.storeuuid = storeuuid;
    }


    /**
     * 入库
     */
    public void doInStore() {
        Emp loginUser = getLoginUser();
        if (null == loginUser) {
            ajaxReturn(false, "请先登录!");
            return;
        }
        try {
            //getId()这里指的是订单明细的id，是前端通过隐藏域传递过来的(orders.html)。
            orderdetailBiz.doInStore(getId(), storeuuid, loginUser.getUuid());
            ajaxReturn(true, "入库成功!");
        } catch (ErpException e) {
            ajaxReturn(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ajaxReturn(false, "入库失败!");
        }
    }


    /**
     * 出库
     */
    public void doOutStore() {
        Emp loginUser = getLoginUser();
        if (null == loginUser) {
            ajaxReturn(false, "请先登录!");
            return;
        }
        try {
            //getId()这里指的是订单明细的id，是前端通过隐藏域传递过来的(orders.html)。
            orderdetailBiz.doOutStore(getId(), storeuuid, loginUser.getUuid());
            ajaxReturn(true, "出库成功!");
        } catch (ErpException e) {
            ajaxReturn(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ajaxReturn(false, "出库失败!");
        }
    }

    /**
     * 根据订单明细ID获取对应的仓库
     */
    public void getStoreByID() {
        try {
            Store store = orderdetailBiz.getStoreByID(getId());
            Map<String, Object> rtn = new HashMap<String, Object>();
            rtn.put("success", true);
            rtn.put("message", "查询仓库成功!");
            rtn.put("data", store);
            String jsonString = JSON.toJSONString(rtn);
            write(jsonString);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> rtn = new HashMap<String, Object>();
            rtn.put("success", false);
            rtn.put("message", "查询仓库失败!");
            String jsonString = JSON.toJSONString(rtn);
            write(jsonString);
        }
    }
}
